import React from 'react'
import { ScrollView } from 'react-native'
import BusInfo from '../components/TimeTables/components/BusInfo';
import BusInfoModel from '../components/Line/models/BusInfoModel';
import { defaultStackOptions } from '../utils/DefaultTitleOption';
import { getSchedules, getPrice } from '../components/TimeTables/api/api';
import TimeTable from '../components/TimeTables/components/TimeTable';
import Error from '../components/public/error/error';

export default class TimeTables extends React.Component {
  static navigationOptions = ({ navigation }) => {
    let option = defaultStackOptions({ navigation })
    option.headerTitle = isNaN(navigation.getParam('name')) ? navigation.getParam('name') : navigation.getParam('name') + '路'
    return option
  }

  constructor(props) {
    super(props)
    this.state = {
      startStation: this.props.navigation.getParam('startstation'),
      endStation: this.props.navigation.getParam('endstation'),
      startTime: this.props.navigation.getParam('starttime'),
      endTime: this.props.navigation.getParam('endtime'),
      type: this.props.navigation.getParam('type'),
      lineId: this.props.navigation.getParam('id'),
      price: '',
      scheduleList: []
    }
  }
  _changeDirction() {
    let startStation = this.state.startStation
    let endStation = this.state.endStation
    if (this.state.type === 1) {
      this.setState({ type: 2, startStation: endStation, endStation: startStation }, ()=>{
        getSchedules(this.state.lineId, this.state.type).then(res=>{
          this.setState({ scheduleList: res.tables })
        })
        getPrice(this.state.lineId, this.state.type).then(res=>{
          this.setState({ price: res.price })
        })
      })
    } else if (this.state.type === 2) {
      this.setState({ type: 1, startStation: endStation, endStation: startStation }, ()=>{
        getSchedules(this.state.lineId, this.state.type).then(res=>{
          this.setState({ scheduleList: res.tables })
        })
        getPrice(this.state.lineId, this.state.type).then(res=>{
          this.setState({ price: res.price })
        })
      })
    }
  }
  componentWillMount() {
    getSchedules(this.state.lineId, this.state.type).then(res=>{
      this.setState({ scheduleList: res.tables })
    })
    getPrice(this.state.lineId, this.state.type).then(res=>{
      this.setState({ price: res.price })
    })
  }
  render() {
    let timeTables = []
    if (this.state.scheduleList.length > 0) {
      this.state.scheduleList.forEach((item, index)=>{
        timeTables.push(<TimeTable key={index + 'timetable'} schedule={item}></TimeTable>)
      })
    } else {
      timeTables.push(<Error tipsText={'还没有时刻表'}></Error>)
    }

    return (
      <ScrollView style={{ height: '100%', backgroundColor: '#F2F5F6' }}>
        <BusInfo price={this.state.price} businfo={new BusInfoModel(this.state.startTime, this.state.endTime, this.state.startStation, this.state.endStation)}
          changeDirction={()=>{ this._changeDirction() }} ></BusInfo>
        {timeTables}
      </ScrollView>
    )
  }
}